package com.pangsir.bookstore.controller;

import com.pangsir.bookstore.pojo.Book;
import com.pangsir.bookstore.pojo.Cart;
import com.pangsir.bookstore.pojo.CartItem;
import com.pangsir.bookstore.pojo.User;
import com.pangsir.bookstore.service.CartItemService;

import javax.servlet.http.HttpSession;

public class CartController {

    private CartItemService cartItemService;

    public String index(HttpSession session){
        User user = (User) session.getAttribute("currUser");
        Cart cart = cartItemService.getCart(user);
        user.setCart(cart);
        session.setAttribute("currUser",user);
        return "cart/cart";
    }


    public String addCart(Integer bookId,HttpSession session){
        User currUser = (User) session.getAttribute("currUser");
        CartItem cartItem = new CartItem(0,new Book(bookId),1,currUser,0.0);
        cartItemService.addOrUpdateCartItem(cartItem,currUser.getCart());
        return "redirect:cart.do";
    }

}
